/**
 * Created by Brook on 2020/4/9.
 */

(function () {
    var async = require('async');
    var moment = require('moment');
    var sql_config = require('../../serverconfig.js');
    var model_sql = require('../../tool/db_helper.js').init(sql_config.Model_Config);
    var SQL_TXT = sql_config.SQL_TXT;
    var urlencode = require('urlencode');



    //接收请求 -  查询中心列队数据 - 取任务
    exports.post_getdata = function(req, res) {
        async.auto({
            check_pars:function (cb){
                res.header('Access-Control-Allow-Origin', '*');
                cb(null, req.body);
            },
            //判断是否有未结束的任务
            check_task:['check_pars', function (cb, result) {
                cb(null, []);
                // model_sql.query(SQL_TXT.format_check_task,[],function(db_err,db_result){
                //     db_err?cb(db_err):cb(null,db_result);
                // });
            }],
            //查询数据
            get_data:['check_task', function (cb, result) {
                if (result.check_task.length ==0) {
                    model_sql.query(SQL_TXT.format_get_data,[],function(db_err,db_result){
                        db_err?cb(db_err):cb(null,db_result);
                    });
                } else {
                    cb(null, result.check_task);
                };
            }],
            //判断中心列队中是否有任务
            check_data:['get_data', function (cb, resulrt) {
                if (resulrt.get_data.length == 0) {
                    var data = {
                        "message": "请求成功",
                        "data": {
                            "code": 21,
                            "message": '中心列表无数据'
                        }
                    };
                    console.log("Response request data success ...")
                    res.json(data);
                    cb('中心列表中无数据')
                } else {
                    cb(null, [])
                };
            }],
            //修改数据状态
            up_data:['check_data', function (cb, result) {
                if (result.check_task.length ==0) {
                    let state = '1', gltfstate = '';
                    if (result.get_data[0].gltfstate == '0') {
                        gltfstate = '0'
                    } else {
                        gltfstate = '2'
                    };
                    model_sql.query(SQL_TXT.format_parsing_up_data,[state,gltfstate,result.get_data[0].id],function(db_err,db_result){
                        db_err?cb(db_err):cb(null,db_result);
                    });
                } else {
                    cb(null, []);
                };
            }],
            //整理输出数据 - 待完善
            output_data:['up_data', function (cb, result) {
                var data = result.get_data;
                if (data.length == 1) {
                    data[0].filePath = urlencode(data[0].filePath);
                };
                cb(null, data);
            }],
            //回传数据
            response_data:['output_data', function (cb, result) {
                var data = {
                    "message": "请求成功",
                    "data": {
                        "code": 200,
                        "data": result.output_data
                    }
                };
                console.log("Response request data success ...")
                res.json(data);
                cb(null, [])
            }]
        },function(err,results){
            if(err){
                console.log(err);
                res.json(r);
            }else{
                var r = {code:0,message:results.check_pars};
                //console.log(r);
            };
        });
    }

    //接收请求 -  转换完成
    exports.post_formatdata = function(req, res) {
        async.auto({
            check_pars:function (cb){
                res.header('Access-Control-Allow-Origin', '*');
                cb(null, req.body);
            },
            //获取任务数据
            query_task:['check_pars', function (cb, result) {
                model_sql.query(SQL_TXT.get_format_task,[result.check_pars.uuid],function(db_err,db_result){
                    db_err?cb(db_err):cb(null,db_result);
                });
            }],
            //判断执行的任务是否存在
            check_task:['query_task', function (cb, result) {
                if (result.query_task.length == 0) {
                    var data = {
                        "message": "请求成功",
                        "data": {
                            "code": 201,
                            "data": '无效的UUID!'
                        }
                    };
                    console.log("Response request data success ...")
                    res.json(data);
                } else {
                    cb(null, result.query_task);
                };
            }],
            //写入数据
            insert_info:['check_task', function (cb, result) {
                var data = result.check_task[0];
                if (data.gltfstate == '0') {
                    let art = urlencode.decode(result.check_pars.filepath).split("/");
                    let model_data = [];
                    model_data = [
                        data.uuid,
                        data.fileid,
                        urlencode.decode(result.check_pars.filepath),
                        data.projectid,
                        data.notifyurl,
                        //art[art.length-1].split(".")[1],
                        "skp",
                        art[art.length-1],
                        data.modelname,
                        data.projectname,
                        data.gltfstate,
                        moment().format('YYYYMMDD'),
                        moment().format('YYYY-MM-DD HH:mm:ss')
                    ];
                    model_sql.query(SQL_TXT.insert_format_info,model_data,function(db_err,db_result){
                        db_err?cb(db_err):cb(null,db_result);
                    });
                } else {
                    model_sql.query(SQL_TXT.up_gltfurl,[urlencode.decode(result.check_pars.filepath),data.uuid],function(db_err,db_result){
                        db_err?cb(db_err):cb(null,db_result);
                    });
                };
            }],
            //修改任务状态
            update_state:['insert_info', function (cb, result) {
                let info = [];
                if (result.check_task[0].gltfstate == '0') {
                    info = ['2','0888','格式转换完成','0', result.check_pars.uuid];
                } else {
                    info = ['0','0888','GlTf生成完成','3', result.check_pars.uuid];
                };

                model_sql.query(SQL_TXT.format_up_state,info,function(db_err,db_result){
                    db_err?cb(db_err):cb(null,db_result);
                });
            }],
            //回传数据
            response_data:['update_state', function (cb, result) {
                var data = {
                    "message": "请求成功",
                    "data": {
                        "code": 200,
                        "data": []
                    }
                };
                console.log("Response request data success ...")
                res.json(data);
                cb(null, [])
            }]
        },function(err,results){
            if(err){
                console.log(err);
                res.json(r);
            }else{
                var r = {code:0,message:[]};
                //console.log(r);
            };
        });
    }

    //接收请求 - 转换失败/异常
    exports.post_formatstate = function(req, res) {
        async.auto({
            check_pars:function (cb){
                res.header('Access-Control-Allow-Origin', '*');
                cb(null, req.body);
            },
            //获取任务数据
            query_task:['check_pars', function (cb, result) {
                model_sql.query(SQL_TXT.get_format_task,[result.check_pars.uuid],function(db_err,db_result){
                    db_err?cb(db_err):cb(null,db_result);
                });
            }],
            //判断执行的任务是否存在
            check_task:['query_task', function (cb, result) {
                if (result.query_task.length == 0) {
                    var data = {
                        "message": "请求成功",
                        "data": {
                            "code": 201,
                            "data": '列表中没有执行中的任务'
                        }
                    };
                    console.log("Response request data success ...")
                    res.json(data);
                } else {
                    cb(null, result.query_task);
                };
            }],
            //修改任务状态
            update_state:['check_task', function (cb, result) {
                let info = [];
                if (result.check_task[0].gltfstate == '0') {
                    info = ['3',result.check_pars.parsingcode,result.check_pars.parsingmessage,'0', result.check_pars.uuid];
                } else {
                    info = ['0',result.check_pars.parsingcode,result.check_pars.parsingmessage,'4',result.check_pars.uuid];
                };
                model_sql.query(SQL_TXT.format_up_state,info,function(db_err,db_result){
                    db_err?cb(db_err):cb(null,db_result);
                });
            }],
            //回传数据
            response_data:['update_state', function (cb, result) {
                var data = {
                    "message": "请求成功",
                    "data": {
                        "code": 200,
                        "data": []
                    }
                };
                console.log("Response request data success ...")
                res.json(data);
                cb(null, [])
            }]
        },function(err,results){
            if(err){
                console.log(err);
                res.json(r);
            }else{
                var r = {code:0,message:[]};
                //console.log(r);
            };
        });
    }

    //接收请求 -  查询中心列队数据 - 取任务
    exports.post_getrevittask = function(req, res) {
        async.auto({
            check_pars:function (cb){
                res.header('Access-Control-Allow-Origin', '*');
                cb(null, req.body);
            },
            //判断是否有未结束的任务
            check_task:['check_pars', function (cb, result) {
                cb(null, []);
                // model_sql.query(SQL_TXT.revittask_check_task,[],function(db_err,db_result){
                //     db_err?cb(db_err):cb(null,db_result);
                // });
            }],
            //查询数据
            get_data:['check_task', function (cb, result) {
                if (result.check_task.length ==0) {
                    model_sql.query(SQL_TXT.revittask_get_data,[],function(db_err,db_result){
                        db_err?cb(db_err):cb(null,db_result);
                    });
                } else {
                    cb(null, result.check_task);
                };
            }],
            //判断中心列队中是否有任务
            check_data:['get_data', function (cb, resulrt) {
                if (resulrt.get_data.length == 0) {
                    var data = {
                        "message": "请求成功",
                        "data": {
                            "code": 21,
                            "message": '中心列表无数据'
                        }
                    };
                    console.log("Response request data success ...")
                    res.json(data);
                    cb('中心列表中无数据')
                } else {
                    cb(null, [])
                };
            }],
            //修改数据状态
            up_data:['check_data', function (cb, result) {
                if (result.check_task.length ==0) {
                    model_sql.query(SQL_TXT.revittask_up_data,['1',result.get_data[0].id],function(db_err,db_result){
                        db_err?cb(db_err):cb(null,db_result);
                    });
                } else {
                    cb(null, []);
                };
            }],
            //整理输出数据 - 待完善
            output_data:['up_data', function (cb, result) {
                var data = result.get_data;
                if (data.length == 1) {
                    data[0].filePath = urlencode(data[0].filePath);
                };
                cb(null, data);
            }],
            //回传数据
            response_data:['output_data', function (cb, result) {
                var data = {
                    "message": "请求成功",
                    "data": {
                        "code": 200,
                        "data": result.output_data
                    }
                };
                console.log("Response request data success ...")
                res.json(data);
                cb(null, [])
            }]
        },function(err,results){
            if(err){
                console.log(err);
                res.json(r);
            }else{
                var r = {code:0,message:results.check_pars};
                //console.log(r);
            };
        });
    }

}).call(this);
